package com.finddeer.bookkeeping;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.finddeer.bookkeeping.fragment.BillFragment;

import java.util.List;
import java.util.UUID;

/**
 * 账单详情页Activity，支持左右滑动手势切换
 * 托管BillFragment（显示账单详情页内容）
 */
public class BillPagerActivity extends AppCompatActivity {

    private static final String EXTRA_BILL_ID =
            "com.finddeer.android.bookkeeping.bill_id";

    private ViewPager mViewPager;
    private List<Bill> mBills;

    public static Intent newIntent(Context packageContext, UUID billId) {
        Intent intent = new Intent(packageContext, BillPagerActivity.class);
        intent.putExtra(EXTRA_BILL_ID, billId);
        return intent;
    }
    //  用以创建附带billId的启动BillPagerActivity的intent

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bill_pager);   //  设置布局文件

        UUID billId = (UUID) getIntent()
                .getSerializableExtra(EXTRA_BILL_ID);

        mViewPager = findViewById(R.id.bill_view_pager);    //  找到viewPager

        mBills = BillLab.get(this).getBills();  //  从单例中获取数据集
        FragmentManager fragmentManager = getSupportFragmentManager();  //  获取activity的FragmentManager实例
        mViewPager.setAdapter(new FragmentStatePagerAdapter(fragmentManager) {
            @Override
            public Fragment getItem(int position) {
                Bill bill = mBills.get(position);
                return BillFragment.newInstance(bill.getId());
            }   //  将fragment添加给activity，而后使用fragment完成工作

            @Override
            public int getCount() {
                return mBills.size();
            }   //  返回列表项数目
        }); //  设置Adapter
        for (int i = 0; i < mBills.size(); i++) {
            if (mBills.get(i).getId().equals(billId)) {
                mViewPager.setCurrentItem(i);
                break;
            }
        }
    }
    //  显示账单详情页的viewPager

}
